package client.control;

import base.entity.info.FileInfo;
import base.entity.regitem.FileRegItem;
import base.service.DirectoriesServer;
import base.service.FileService;
import base.service.impl.DirectoriesServerImpl;
import base.view.DirectoriesView;
import base.view.View;
import com.sun.org.apache.xml.internal.security.Init;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import client.util.StageManager;

import java.awt.*;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class ShowFileController implements Initializable {

    public Stage stage = StageManager.STAGE.get("showFile");
    public TextArea oldFileContent;
    public TextArea newFileContent;
    public FileService fileService;
    public String fileUrl;
    public String fileName;


    public void surePeservation(ActionEvent event) {

        System.out.println(oldFileContent.getText().trim() + newFileContent.getText().trim());
        //传入保存后的文件内容

        String fileContent = null;
        try {
            System.out.println("绝对路径：" + fileUrl);
            fileService.writeFile(oldFileContent.getText().trim() + newFileContent.getText().trim(),fileUrl);
            fileService.closeFile(fileUrl);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //推出前要关闭文件
        //fileService.closeFile(fileUrl + "." + fileType);
        StageManager.STAGE.get("showFile").close();
        //删除map中的引用
        StageManager.STAGE.remove("showFile");
        IndexController controller = (IndexController) StageManager.CONTROLLER.get("indexControl");
        controller.searching = false;
        controller.show(controller.url);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        StageManager.CONTROLLER.put("showFileControl", this);
    }

    public void getFileContent(String url, String fileName, FileService fileService) {
        System.out.println("打开文件的绝对路径和文件名：" + fileUrl);
        //传入文件的绝对路径，返回文件的内容
        this.fileService = fileService;
        this.fileUrl = url + "/" + fileName;
        this.fileName = fileName;
        String fileContent = null;
        String fileType = null;
        FileRegItem fileRegItem = fileService.searchFileByPath(fileUrl);
        boolean oR = (fileRegItem.getProperty() & View.ONLY_READABLE) > 0;
        boolean readAndWriteAble = (fileRegItem.getProperty() & View.READABLE_AND_WRITEABLE) > 0;
        if (readAndWriteAble) {
            newFileContent.setVisible(true);
            newFileContent.setText("");
        }
        else if (oR) newFileContent.setVisible(false);
        try {
            DirectoriesView directoriesView = new DirectoriesServerImpl().dir(url);
            List<FileInfo> fileInfos = directoriesView.getChildFileInfo();
            for (FileInfo fileInfo:fileInfos){
                if (fileInfo.getName().equals(fileName)){
                    fileType = fileInfo.getType();
                }
            }
            System.out.println("绝对路径：" + fileUrl);
            fileService.openFileByRegItem(url, fileRegItem);
            fileContent = fileService.readFile(fileUrl);

        } catch (Exception e) {
            e.printStackTrace();
        }
        oldFileContent.setText(fileContent);
        newFileContent.setDisable(false);
    }

}
